<template>
	<view class="order-list">
		<view class="order-list-container">
			<view class="header">
				<view class="title">
					订单信息
					<text>消费订单：0 总消费：￥0</text>
				</view>
				<image src="/static/images/orderTime.png"></image>
			</view>
			<view class="main">
				<van-tabs ref="vanTabs" @change="onOrderTabChange">
					<van-tab
						v-for="tab in orderStatus"
						:key="tab.type"
						:title="tab.name">
						<view class="list">
							<view
								v-for="order in orderList"
								:key="order.id"
								class="item">
								<view class="item__header">
									<text class="createtime">2021-09-09 22:21:53</text>
									<text class="status">待付款</text>
								</view>
								<view class="item__body">
									<view
										v-for="goods in order.cartInfo"
										:key="goods.id"
										class="order-goods">
										<view class="base-info">
											<image class="cover"
												:src="goods.productInfo.image">
											</image>
											<view class="name">{{ goods.productInfo.store_name }}</view>
											<view class="price">
												{{ goods.truePrice }}
												<text class="num">x{{ goods.cart_num }}</text>
											</view>
										</view>
									</view>
									<view class="sub-total">
										共{{ order.cart_id.length }}件商品，总金额<text>￥{{ order.total_price }}</text>
									</view>
								</view>
								<view class="item__footer">
									<button class="cancel-btn">取消订单</button>
									<button class="repay-btn">立即付款</button>
								</view>
							</view>
						</view>
						<Empty v-if="noneOrder"></Empty>
						<NoMore v-show="noMore"></NoMore>
					</van-tab>
				</van-tabs>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getOrderList as getOrderListApi
	} from '@/api/order'
	import {
		orderStatus
	} from '@/config/order'
	import VanTabs from '@/wxcomponents/vant/tabs/index'
	import VanTab from '@/wxcomponents/vant/tab/index'
	import NoMore from '@/components/NoMore'
	import Empty from './components/Empty'
	export default {
		components: {
			VanTabs,
			VanTab,
			Empty,
			NoMore
		},
		data() {
			return {
				orderStatus,
				params: {
					type: 0,
					page: 1,
					limit: 20,
				},
				orderList: [],
				noMore: false
			}
		},
		computed: {
			noneOrder () {
				return this.orderList.length === 0
			}
		},
		onShow () {
			this.getOrderList()
		},
		onReachBottom () {
			this.getOrderList()
		},
		methods: {
			onOrderTabChange () {
				this.params.type = this.$refs.vanTabs.data.currentIndex
				this.params.page = 1
				this.noMore = false
				this.getOrderList()
			},
			async getOrderList() {
				if (this.noMore) {
					return false
				}
				const {
					status,
					data,
					msg
				} = await getOrderListApi(this.params)
				if (status === this.API_STATUS_CODE.SUCCESS) {
					if (this.params.page > 1) {
						this.orderList = [...this.orderList, ...data]

						if (data.length === 0) {
							this.noMore = true
						}
					} else {
						this.orderList = data
					}

					if (data.length) {
						this.params.page++
					}
				} else {
					uni.showToast({
						icon: 'none',
						title: msg,
						duration: 3000
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.order-list {
		min-height: 100vh;
		background-color: #f5f5f5;

		&-container {
			.header {
				display: flex;
				justify-content: space-between;
				height: 260rpx;
				padding: 72rpx 30rpx 0;
				background-color: #e93323;
				box-sizing: border-box;

				.title {
					display: flex;
					flex-direction: column;
					font-size: 34rpx;
					font-weight: 700;
					color: #fff;

					text {
						color: hsla(0, 0%, 100%, .8);
						font-size: 26rpx;
						font-family: Guildford Pro;
						margin-top: 20rpx;
					}
				}

				image {
					width: 122rpx;
					height: 108rpx;
				}
			}

			.main {
				width: 690rpx;
				margin: 14rpx auto 0 auto;

				::v-deep .van-tabs {
					margin-top: -72rpx;

					.list {
						.item {
							margin-top: 14rpx;
							background-color: #fff;

							&__header {
								display: flex;
								justify-content: space-between;
								align-items: center;
								height: 84rpx;
								padding: 0 30rpx;
								border-bottom: 1rpx solid #eee;
								font-size: 28rpx;
								color: #282828;
							}

							&__body {
								padding: 0 30rpx;
								margin-top: 22rpx;
								.order-goods {
									padding: 0 30rpx;
									margin-top: 22rpx;
								}

								.base-info {
									display: flex;

									.cover {
										width: 120rpx;
										height: 120rpx;
									}

									.name {
										width: 309rpx;
										color: #282828;
										display: -webkit-box;
										-webkit-line-clamp: 2;
										-webkit-box-orient: vertical;
										overflow: hidden;
										word-break: break-all;
									}

									.price {
										flex: 1;
										display: flex;
										flex-direction: column;
										align-items: flex-end;
									}
								}

								.sub-total {
									font-size: 26rpx;
									color: #282828;
									text-align: right;
									margin: 26rpx 0 0 30rpx;
									padding: 0 30rpx 30rpx 0;
									border-bottom: 1rpx solid #eee;

									text {
										font-size: 28rpx;
										font-weight: 700;
										color: #fc4141 !important;
									}
								}
							}

							&__footer {
								display: flex;
								justify-content: flex-end;
								align-items: center;
								height: 106rpx;
								padding: 0 30rpx;

								button {
									width: 176rpx;
									height: 60rpx;
									text-align: center;
									line-height: 60rpx;
									border-radius: 50rpx;
									font-size: 26rpx;
									margin: 0;
									margin-left: 16rpx;

									&.cancel-btn {
										color: #aaa;
										border: 1rpx solid #ddd;
									}

									&.repay-btn {
										color: #fff;
										background-color: #e93323 !important;
										border: 1rpx solid #e93323;
									}
								}
							}
						}
					}
				}
			}
		}
	}
</style>
